;//
//  PlayerProtocol.m
//  TabulaImaginarium11
//
//  Created by TA2 on 6/20/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PlayerProtocol.h"


@implementation PlayerProtocol

- (void)readPacket:(NSArray *)packet fromSocket:(AsyncSocket *)socket {
	NSArray *arguments = packet;
	if ([arguments count] > 1) {
		if ([[arguments objectAtIndex:1] isEqualToString:@"RAP"]) {
			for (Player *player in [Players sharedAllPlayers]) {
                if (player.alias) {
                    [self sendPlayer:player.ip withAlias:player.alias];
                } else {
                   [self sendPlayer:player.ip withAlias:@"nil"]; 
                }
			}
			[self sendPlayer:[Players sharedLocalPlayer].ip withAlias:[Players sharedLocalPlayer].alias];
		} else if ([[arguments objectAtIndex:1] isEqualToString:@"SP"]) {
            NSString *ip = [arguments objectAtIndex:2];
            if ([ip isEqualToString:@"nil"]) {
                ip = nil;
            }
            NSString *alias = [arguments objectAtIndex:3];
            if ([alias isEqualToString:@"nil"]) {
                alias = nil;
            }
			if (!([[Players sharedLocalPlayer].ip isEqualToString:ip] || [[Players sharedPlayers] containsObject:ip])) {
				Player *player = [[[Player alloc] init] autorelease];
				player.ip = ip;
                player.alias = alias;
				[[Players sharedPlayers] addObject:player];
			}
		} else if ([[arguments objectAtIndex:1] isEqualToString:@"RGN"]) {
            [self sendGameName];
		} else if ([[arguments objectAtIndex:1] isEqualToString:@"SGN"]) {
				contentView.tokenView.map.name = [arguments objectAtIndex:2];
		} else if ([[arguments objectAtIndex:1] isEqualToString:@"RPOS"]) {
            [self sendPositionToSocket:socket];
		} else if ([[arguments objectAtIndex:1] isEqualToString:@"SPOS"]) {
            if (![Players sharedLocalPlayer].isHost) {
                [contentView setContentOffset:CGPointFromString([arguments objectAtIndex:2]) animated:NO];
            }
		} else if ([[arguments objectAtIndex:1] isEqualToString:@"CA"]) {
            if ([Players sharedLocalPlayer].isHost) {
                NSString *dataString = [[[stringTag stringByAppendingString:@"|CA|"] stringByAppendingString:NSStringFromCGPoint(contentView.contentOffset)] stringByAppendingString:@"|≤|"];
                NSData *data = [dataString dataUsingEncoding: NSUTF8StringEncoding];
                [[NetworkController sharedNetworkController] sendDataToAll:data withTag:0 butSocket:socket];
            }
            [contentView.backgroundView.map clearMap];
            [contentView.backgroundView setNeedsDisplay];
            [contentView.drawingView setNeedsDisplay];
            [contentView.tokenView setNeedsDisplay];
            [contentView.objectView setNeedsDisplay];
        }
	}
}

- (void)sendRequestForAllPlayers {
	NSString *dataString = [[stringTag stringByAppendingString:@"|RAP"] stringByAppendingString:@"|≤|"];
	NSData *data = [dataString dataUsingEncoding: NSUTF8StringEncoding];
	[[NetworkController sharedNetworkController] sendDataToAll:data withTag:0];
}

- (void)sendPlayer:(NSString *) ip withAlias:(NSString *) alias {
    if (!alias) {
        alias = @"nil";
    }
    if (!ip) {
        ip = @"nil";
    }
	NSString *dataString = [[[[[stringTag stringByAppendingString:@"|SP|"] stringByAppendingString:ip] stringByAppendingString:@"|"] stringByAppendingString:alias] stringByAppendingString:@"|≤|"];
	NSData *data = [dataString dataUsingEncoding: NSUTF8StringEncoding];
	[[NetworkController sharedNetworkController] sendDataToAll:data withTag:0];
}

- (void)sendRequestForGameName {
    NSString *dataString = [[stringTag stringByAppendingString:@"|RGN"] stringByAppendingString:@"|≤|"];
	NSData *data = [dataString dataUsingEncoding: NSUTF8StringEncoding];
	[[NetworkController sharedNetworkController] sendDataToAll:data withTag:0];
}

- (void)sendGameName {
    NSString *dataString = [[[stringTag stringByAppendingString:@"|SGN|"] stringByAppendingString:contentView.tokenView.map.name] stringByAppendingString:@"|≤|"];
	NSData *data = [dataString dataUsingEncoding: NSUTF8StringEncoding];
	[[NetworkController sharedNetworkController] sendDataToAll:data withTag:0];
}

- (void)sendRequestForPosition {
    NSString *dataString = [[stringTag stringByAppendingString:@"|RPOS"] stringByAppendingString:@"|≤|"];
	NSData *data = [dataString dataUsingEncoding: NSUTF8StringEncoding];
	[[NetworkController sharedNetworkController] sendDataToAll:data withTag:0];
}

- (void)sendPositionToSocket:(AsyncSocket *)socket {
    NSString *dataString = [[[stringTag stringByAppendingString:@"|SPOS|"] stringByAppendingString:NSStringFromCGPoint(contentView.contentOffset)] stringByAppendingString:@"|≤|"];
	NSData *data = [dataString dataUsingEncoding: NSUTF8StringEncoding];
    [[NetworkController sharedNetworkController] sendData:data toSocket:socket withTag:0];
	//[[NetworkController sharedNetworkController] sendDataToAll:data withTag:0];
}

- (void)sendClearCommand {
    NSString *dataString = [[[stringTag stringByAppendingString:@"|CA|"] stringByAppendingString:NSStringFromCGPoint(contentView.contentOffset)] stringByAppendingString:@"|≤|"];
    NSData *data = [dataString dataUsingEncoding: NSUTF8StringEncoding];
    [[NetworkController sharedNetworkController] sendDataToAll:data withTag:0];
}

//TODO
//Send host to connecting player
- (void)setupProtocol {
	if (![Players sharedLocalPlayer].isHost) {
        [contentView.backgroundView.map clearMap];
        [self sendPlayer:[Players sharedLocalPlayer].ip withAlias:[Players sharedLocalPlayer].alias];
        [self sendRequestForAllPlayers];
        [self sendRequestForGameName];
	}
	[contentView.backgroundView setNeedsDisplay];
	[contentView.drawingView setNeedsDisplay];
	[contentView.tokenView setNeedsDisplay];
    [contentView.objectView setNeedsDisplay];
}


@end
